package com.gltc.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gltc.constant.Constant;
import com.gltc.entities.PhotoAuthUpload;
import com.gltc.entities.PhotoAuthUser;
import com.gltc.service.PhotoAuthUploadService;
import com.gltc.service.PhotoAuthUserService;
import com.gltc.util.DateUtil;
import com.gltc.util.PageUtils;

@Controller
@RequestMapping("/")
public class PhotoAuthUserController {
	
	@Autowired
	private PhotoAuthUserService userLogRegService;
	
	@Autowired
	private PhotoAuthUploadService authUploadService;

	/**
	 * 跳转主页，拦截器实现
	 * @return
	 */
	@RequestMapping("/userlog/index")
	public String index(){
		return "/jsps/login.jsp";
	}
	
	
	/**
	 * 登录
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/userlog/login")
	public String login(PhotoAuthUser userLogReg, HttpServletRequest request){
		
		if(userLogReg == null){
			return "faile";
		}
		PhotoAuthUser user = userLogRegService.checkLoginName(userLogReg);
		
		if(user != null){
			request.getSession().setAttribute(Constant.SESSION_USER_KEY, user);
			userLogRegService.updateUserLoginTime(user.getId(),DateUtil.formatDateTime(new Date()));
			if (user.getIsadmin() == 1) { //管理员
				return "1";
			}else if (user.getIsadmin() == 2) { //照片审核员
				return "2";
			}
		}
		return "userNull";
	}
	
	
	/**
	 * 跳转到管理员页面
	 * @param userLogReg
	 * @param result
	 * @return
	 */
	@RequestMapping("/toAdminPage")
	public String toAdminPage(Model model, HttpServletRequest request, 
			@RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "10") int size){
		
		PhotoAuthUser user = (PhotoAuthUser) request.getSession().getAttribute(Constant.SESSION_USER_KEY);
		if( user == null ){
			return "redirect:/userlog/index";
		}
		
		
		//管理员页面查看审核合格的记录
		List<PhotoAuthUpload> photoLogformLog = authUploadService.getPhotoLogformLog((page-1)*size, size);
		model.addAttribute("photoLogformLog", photoLogformLog);
		
		//分页
		String pageIterator = PageUtils.getPageIterator(request, authUploadService.getPhotoLogCount(), page, size);
		model.addAttribute("pageIterator", pageIterator);
		
		return "/jsps/authphotolist.jsp";
	}
	
	
	/**
	 * 跳转到照片审核员页面
	 * @param userLogReg
	 * @param result
	 * @return
	 */
	@RequestMapping("/toPhotoPage")
	public String toPhotoPage(HttpServletRequest request){
			
		PhotoAuthUser user = (PhotoAuthUser) request.getSession().getAttribute(Constant.SESSION_USER_KEY);
		if( user == null ){
			return "redirect:/userlog/index";
		}
			return "/jsps/uploadPhoto.jsp";
		}
	
	/**
	 * 注册
	 * @return
	 *//*
	@RequestMapping("/userlog/regist")
	public String regist(UserLogReg userLogReg, HttpServletRequest result){
		
		if(userLogReg != null){
			userLogReg.setRegisterTime(DateUtil.formatDateTime(new Date()));
		}
		
		@SuppressWarnings("unused")
		boolean b = userLogRegService.insertUser(userLogReg);
		
		return "/cn/hy/login/register.jsp";
	}*/
	
	/**
	 * 检测用户名是否被占用
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/userlog/checkUnique")
	public Map<String, Boolean> checkUnique(@RequestParam(defaultValue="")String userName, @RequestParam(defaultValue="0")int userId,  HttpServletRequest result){
		 Map<String, Boolean> map = new HashMap<String, Boolean>();
	        PhotoAuthUser user = userLogRegService.getUserByLoginName(userName);
	        //用户不存在，校验有效
	        if (user == null) {
	            map.put("valid", true);
	        }else {
	            map.put("valid", false);
	        }
 	        return map;
	}
}
